package slidingwindow;

//最⼤连续 1 的个数 III
public class LongestOne {
    public static void main(String[] args) {
        int[] nums = {1,1,1,0,0,0,1,1,1,1,0};
        LongestOne longestOne = new LongestOne();
        System.out.println(longestOne.longestOnes(nums, 2));
    }

    public int longestOnes(int[] nums, int k) {
        int n = nums.length, ret = 0;
        for (int right = 0, left = 0, zero = 0; right < n; right++) {
            if (nums[right] == 0) zero++;
            while (zero > k) {
                if (nums[left++] == 0) zero--;
            }
            ret = Math.max(ret, right - left + 1);
        }
        return ret;
    }


//    public int longestOnes(int[] nums, int k) {
//        int n = nums.length;
//        int ret = 0;
//        for (int right = 0, left = 0, count = 0; right < n; right++) {
//            if (nums[right] == 0) {
//                count++;
//            }
//                while (count > k) {
//                    if (nums[left++] == 0) {
//                        count--;
//                    }
//                }
//            ret = Math.max(ret, right - left + 1);
//
//        }
//        return ret;
//    }
}
